/**
 * @description  : 一键搜搜索页面
 * @descriptionDetail : 一键搜搜索页面
 * @copyright    : 浙江烟草
 * @author       : mc
 * @create       : 2022-11-03 17:35:18
 */
<template>
  <view class="page">
    <!-- #ifdef H5-ONLY -->
    <theme-navbar :isWhite="false" backText="返回" title="一键搜"></theme-navbar>
    <!-- #endif -->
    <view class="key-search-lay">
      <view class="key-search">
        <u-search
          bg-color="#fff"
          shape="round"
          :clearabled="true"
          :showAction="true"
          actionText="确定"
          placeholder="请输入要查询的内容"
          v-model="searchKey"
          @custom="toSearchList($event, false)"
        ></u-search>
      </view>
    </view>
    <view class="content u-m-b-16 u-m-l-5">
      <choice-tag :allowEmptyChoice="false" :customStyle="customStyle" :btnList="dataTypes" :single="false" :tapListener="tapListener"></choice-tag>
    </view>
    <!-- 历史搜索 -->
    <view class="key-history">
      <view class=" content">
        <view class="key-history-title">
          <text>历史搜索</text>
          <u-image width="32" height="32" src="/static/keySearch/clear-history.png" mode="widthFix" @click="clearHistory"></u-image>
        </view>
        <view class="key-btn u-m-l-16 u-m-r-16 u-m-t-16">
          <text class="btn-desc" v-for="(item,index) in historyList" :key="index" @click="toSearchList(item.title,true)">
            {{ item.title }}
          </text>
        </view>
      </view>
    </view>
  </view>
</template>

<script> 
import choiceTag from '@/components/choice_tag.vue';
import {INFORMATION_ANALYSIS_DATA,} from "@/service/infoAnalysis/mapping/enum.js";
import searchHistoryService from "@/service/utilities/searchHistory.service.js";
import {
  v4 as uuidv4,
} from 'uuid';
export default {
  components: { 
    choiceTag,
  },
  data() {
    return {
      customStyle: {
        "margin-left": "5px",
        "margin-right": "5px",
        "padding-left": "20px",
        "padding-right": "20px",
        "margin-bottom": "10px",
      },
      dataTypes: INFORMATION_ANALYSIS_DATA, // 
      searchKey:'',
      selCodes:'',
      preCodes:'',// 一键搜首页传过来的参数
      historyList:[],
      TAG_INFOMATION_ANALYSIS : "infomation_analysis",
      userInfo:this.$storage.get(this.$rp.common.USER).user,
    };
  }, 
  onLoad(options){
    this.preCodes = options.type;
    this.dataTypes.map(item => {
      item['isChoice'] = this.preCodes && this.preCodes.includes(item.code);
    });
  },
  mounted() {
  },
  onShow() {
    this.getHistoryData();
  },
  methods: {
    // 历史搜索数据 personUuid
    getHistoryData(){
      searchHistoryService.getKeySearchData(this.userInfo.personUuid,this.TAG_INFOMATION_ANALYSIS).then(res => {
        let [obj,newArry,] = [{},[],];
        if(res && res.length > 0){ // 去重
          newArry = res.reduce((item,next) => {
            obj[this.$u.trim(next.title, 'all')] ? "" : obj[this.$u.trim(next.title, 'all')] = true && item.push(next);
            return item;
          },[]);
        }
        this.historyList = newArry;
      });
    },
    toSearchList(keys,flag){
      console.log(this.dataTypes);
      if(!flag){
        if(this.$u.test.isEmpty(this.$u.trim(this.searchKey, 'all'))){
          this.$u.toast('请输入要查询的内容！');
          return;
        }
        this.saveHistory(this.searchKey);
      }
    
      this.setRouteParam({
        type:this.preCodes ,
        searchKey: flag ? keys : this.searchKey,
      });
      this.$u.route('pages/keySearch/searchKeyList');
    },
    // 历史数据保存本地
    saveHistory(searchKey){
      let params = {
        title : searchKey,
        context : searchKey,
        type : this.TAG_INFOMATION_ANALYSIS,
        userId : this.userInfo.personUuid,
        uuid : uuidv4(),
      }
      searchHistoryService.saveIndexDBKeySearch(params).then(res => {
      });
    },
    // 清空历史搜索
    clearHistory(){
      this.$modal.confirm(`确定清空历史数据吗？`, () => {
        searchHistoryService.clearKeySearch().then(() => {
          this.getHistoryData();
        });
      });
    },
    // 监听多选操作
    tapListener(arr){
      let dataCode = [];
      if (arr.length > 0) {
        arr.forEach(item => {
          dataCode.push(item.code);
        });
      }
      this.preCodes = dataCode.join(",");
    },
  },
};
</script>

<style lang="scss" scoped>
.page{ padding:0; }
.key-search-lay{
  .key-search{
    width: 100%;
	padding: $cm-search-padding-row $cm-search-padding-column;
  }
}
.key-btn{
  margin-bottom: 20px;
  .btn-desc{
    background:#f5f5f5 ;
    padding: 12rpx 32rpx;
    display: inline-block;
    margin: 8px;
    border-radius: 6px;
  }
}
.key-history-title{
    display: flex;
    justify-content: space-between;
	width: 100%;
	background: #F3F4F5;
	height: 64rpx;
	padding: 5px 10px;
    text{
      font-size: 14px;
      color: #989EA1;
    }
  }
.content{
  width:100%;
  margin:auto;
}
</style>
